<?php
namespace App\Http\Controllers\Weixin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntMemberInfo;
use Session;
use Redirect, Validator, Cookie, Response, DB;
use App\Http\Controllers\Weixin\WeixinServiceController as weixinService;
use EasyWeChat\Payment\Order;


class WeiXinController extends Controller
{
    protected $scaleOne = '0.3';
    protected $scaleTwo = '0.05';
    protected $scaleMoney = '2000';


    /*
     * 本页面仅仅限于微信端进入
     * */
    public function music(Request $request)
    {

        $parentID       = $request->input('state');
        $openId         = $request->session()->get('openId');
        if(empty($openId)){ //用户没有登录

            $code           = $request->input('code');
            $weixinService  = new weixinService();
            $callbackUrl    = "music.html?state=".$parentID;
            $wxapp          = $weixinService->get_weixin_app(1,$callbackUrl);
            $oauth          = $wxapp->oauth;
            if(empty($code)){ //缺少code
                return $oauth->redirect();
            }else{
                $user   = $oauth->user()->toArray();
                $openid = $user['id'];

                if(empty($openid)){
                    return '获取微信ID失败';
                }
                $request->session()->put('openId',$openid);
            }
        }

        if ($parentID == '3') {
            return redirect('/testpay/paymax.html');
        }

        $openId = $request->session()->get('openId');
        $memberInfo = $this->checkMemberInfo(2, $openId);

        /** 不存在该微信用户 * */
        if (count($memberInfo) == 0) {
            if ($parentID <= 2) {
                return redirect('/login.html');
            } else {
                return redirect('/parent/' . $parentID . '.html');
            }
        }

        $loginKey = md5(date('YmdHis', time()));
        Session::put('MName', $memberInfo->UserName);
        Session::put('MKey', $loginKey);

        //等于2表示是合伙人
        if ($parentID == '2') {
            EntMemberInfo::where('MemNumber', $memberInfo->MemNumber)
                ->where('States', 1)
                ->update(['LoginKey' => $loginKey]);
            Session::put('MName', $memberInfo->UserName);
            Session::put('MKey', $loginKey);
            return redirect('/partner/index.html');
        }



        //判断是否已经缴纳会费
        if ($memberInfo->States == 0) {
            EntMemberInfo::where('MemNumber', $memberInfo->MemNumber)
                ->where('States', 0)
                ->update(['LoginKey' => $loginKey]);
            return redirect('/pay/pay.html');
        } else {
            $zero1 = date('Y-m-d H:i:s');
            $zero2 = $memberInfo->MemEndTime;
            if (strtotime($zero1) < strtotime($zero2)) {
                //未到期
                EntMemberInfo::where('MemNumber', $memberInfo->MemNumber)->where('States', 1)->update(['LoginKey' => $loginKey]);
                return redirect('/book.html');
            } else {
                //到期
                EntMemberInfo::where('MemNumber', $memberInfo->MemNumber)->where('States', 1)->update(['LoginKey' => $loginKey]);
                return redirect('/pay/pay.html');
            }
        }
    }




}

